<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="协议名称" prop="protocolName">
          <el-input
            v-model="queryParams.protocolName"
            placeholder="请输入协议名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="协议类型" prop="protocolType">
          <el-select v-model="queryParams.protocolType" placeholder="请选择协议类型" clearable>
            <el-option
              v-for="dict in dict.type.modality_list"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:configuration:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:configuration:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:configuration:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['system:configuration:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="configurationList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />

        <el-table-column label="协议名称" align="center" prop="protocolName" />
        <el-table-column label="协议类型" align="center" prop="protocolType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.modality_list" :value="scope.row.protocolType"/>
          </template>
        </el-table-column>
<!--        <el-table-column label="单位" align="center" prop="unit" />-->
<!--        <el-table-column label="量程" align="center" prop="ranges" />-->
        <el-table-column label="波特率" align="center" prop="baud" />
        <el-table-column label="数据位" align="center" prop="dataBit" />
        <el-table-column label="校验位" align="center" prop="checkBit" />
        <el-table-column label="停止位" align="center" prop="stopBit" />
        <el-table-column label="地址码" align="center" prop="addressCode" />
        <el-table-column label="功能码" align="center" prop="functionCode" />
<!--        <el-table-column label="寄存器地址" align="center" prop="registerAddress" />-->
<!--        <el-table-column label="寄存器数量" align="center" prop="registerNumber" />-->
        <el-table-column label="寄存器字节数" align="center" prop="registerBit" />
<!--        <el-table-column label="有无符号" align="center" prop="withoutSymbols" />-->
<!--        <el-table-column label="乘以系数" align="center" prop="multiplyCoefficient" />-->
<!--        <el-table-column label="延时时间(秒)" align="center" prop="delayTime" />-->
<!--        <el-table-column label="显示或报警" align="center" prop="deviceStatus" />-->
<!--        <el-table-column label="灯的颜色" align="center" prop="lightColor" />-->
<!--        <el-table-column label="0有效同闭合或1有效同断开" align="center" prop="closeDisconnect" />-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['system:configuration:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['system:configuration:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改设备协议配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="200px">

          <el-form-item label="协议类型" prop="protocolType">
            <el-select v-model="form.protocolType" placeholder="请选择协议类型">
              <el-option
                v-for="dict in dict.type.modality_list"
                :key="dict.value"
                :label="dict.label"
  :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="协议名称" prop="protocolName">
            <el-input v-model="form.protocolName" placeholder="请输入协议名称" />
          </el-form-item>
<!--          <el-form-item label="单位" prop="unit">-->
<!--            <el-input v-model="form.unit" placeholder="请输入单位" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="量程" prop="ranges">-->
<!--            <el-input v-model="form.ranges" placeholder="请输入量程" />-->
<!--          </el-form-item>-->
          <el-form-item label="波特率" prop="baud">
            <el-input v-model="form.baud" placeholder="请输入波特率" />
          </el-form-item>
          <el-form-item label="数据位" prop="dataBit">
            <el-input v-model="form.dataBit" placeholder="请输入数据位" />
          </el-form-item>
          <el-form-item label="校验位" prop="checkBit">
            <el-input v-model="form.checkBit" placeholder="请输入校验位" />
          </el-form-item>
          <el-form-item label="停止位" prop="stopBit">
            <el-input v-model="form.stopBit" placeholder="请输入停止位" />
          </el-form-item>
          <el-form-item label="地址码" prop="addressCode">
            <el-input v-model="form.addressCode" placeholder="请输入地址码" />
          </el-form-item>
          <el-form-item label="功能码" prop="functionCode">
            <el-input v-model="form.functionCode" placeholder="请输入功能码" />
          </el-form-item>
<!--          <el-form-item label="寄存器地址" prop="registerAddress">-->
<!--            <el-input v-model="form.registerAddress" placeholder="请输入寄存器地址" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="寄存器数量" prop="registerNumber">-->
<!--            <el-input v-model="form.registerNumber" placeholder="请输入寄存器数量" />-->
<!--          </el-form-item>-->
          <el-form-item label="寄存器字节数" prop="registerBit">
            <el-input v-model="form.registerBit" placeholder="请输入寄存器字节数" />
          </el-form-item>
<!--          <el-form-item label="有无符号" prop="withoutSymbols">-->
<!--            <el-input v-model="form.withoutSymbols" placeholder="请输入有无符号" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="乘以系数" prop="multiplyCoefficient">-->
<!--            <el-input v-model="form.multiplyCoefficient" placeholder="请输入乘以系数" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="延时时间(秒)" prop="delayTime">-->
<!--            <el-input v-model="form.delayTime" placeholder="请输入延时时间(秒)" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="灯的颜色" prop="lightColor">-->
<!--            <el-input v-model="form.lightColor" placeholder="请输入灯的颜色" />-->
<!--          </el-form-item>-->
<!--          <el-form-item label="0有效同闭合或1有效同断开" prop="closeDisconnect">-->
<!--            <el-input v-model="form.closeDisconnect" placeholder="请输入0有效同闭合或1有效同断开" />-->
<!--          </el-form-item>-->
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </template>

  <script>
  import { listConfiguration, getConfiguration, delConfiguration, addConfiguration, updateConfiguration } from "@/api/system/configuration";

  export default {
    name: "Configuration",
    dicts: ['modality_list'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 设备协议配置表格数据
        configurationList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          protocolName: null,
          protocolType: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询设备协议配置列表 */
      getList() {
        this.loading = true;
        listConfiguration(this.queryParams).then(response => {
          this.configurationList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          protocolName: null,
          protocolType: null,
          unit: null,
          ranges: null,
          baud: null,
          dataBit: null,
          checkBit: null,
          stopBit: null,
          addressCode: null,
          functionCode: null,
          registerAddress: null,
          registerNumber: null,
          registerBit: null,
          withoutSymbols: null,
          multiplyCoefficient: null,
          delayTime: null,
          deviceStatus: "0",
          lightColor: null,
          closeDisconnect: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加设备协议配置";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getConfiguration(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改设备协议配置";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateConfiguration(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addConfiguration(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除设备协议配置编号为"' + ids + '"的数据项？').then(function() {
          return delConfiguration(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/configuration/export', {
          ...this.queryParams
        }, `configuration_${new Date().getTime()}.xlsx`)
      }
    }
  };
  </script>
